package com.ywjt.platformbusiness.classify;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseVPFragment;
import com.ywjt.platformbusiness.classify.adapter.ClassifyTypeAdapter;
import com.ywjt.platformbusiness.classify.fragment.ClassifyDesignerFragment;
import com.ywjt.platformbusiness.classify.fragment.ClassifyFragment;
import com.ywjt.platformbusiness.classify.model.FistLevelTypeModel;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.home.CommonModel;
import com.ywjt.platformbusiness.home.activity.Activity_Search_History;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.my.adapter.CouponAdapter;
import com.ywjt.platformbusiness.my.model.MyCouponListModel;
import com.ywjt.platformbusiness.util.JsonUtils;
import com.ywjt.platformbusiness.widget.BaseViewPager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClassifyIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private RecyclerView rcType;
    private View view;
    private BaseViewPager viewPager;
    private MainAdapter adapter;
    private List<Fragment> fragmentList;
    private List<CommonModel> mTitleList;
    private ClassifyTypeAdapter typeAdapter;
    private LinearLayout llSearch;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llSearch:
                Activity_Search_History.actionStart(context);
                break;
        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) { }

    @Override
    protected void onFragmentFirstVisible() {
        getData();
    }

    public static ClassifyIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
     ClassifyIndex fl = new ClassifyIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.classify_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        rcType=view.findViewById(R.id.rcType);
        viewPager=view.findViewById(R.id.viewPager);
        llSearch=view.findViewById(R.id.llSearch);
        mTitleList=new ArrayList<>();
        fragmentList=new ArrayList<>();

        llSearch.setOnClickListener(this::onClick);
    }

    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position).getStr();
        }
    }


    //获取一级分类
    public void getData(){
        new HttpRequest(getContext()).doGet(UrlConstants.getFirstClassify, "", new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                //把请求得数据转换成json对象
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), FistLevelTypeModel.class);
                        if (obj instanceof FistLevelTypeModel) {
                            FistLevelTypeModel levelTypeModel =(FistLevelTypeModel)obj;
                            mTitleList.add(new CommonModel("专属设计师",true));
                            fragmentList.add(ClassifyDesignerFragment.newInstance(""));
                            for (int i=0;i<levelTypeModel.getResult().size();i++){
                                mTitleList.add(new CommonModel(levelTypeModel.getResult().get(i).getName(),false));
                                fragmentList.add(ClassifyFragment.newInstance(levelTypeModel.getResult().get(i).getId()));
                            }
                            adapter=new MainAdapter(getChildFragmentManager());
                            viewPager.setAdapter(adapter);
                            viewPager.setScrollable(false);
                            typeAdapter=new ClassifyTypeAdapter(mTitleList);
                            rcType.setAdapter(typeAdapter);
                            rcType.setLayoutManager(new LinearLayoutManager(context));

                            typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    for (int i=0;i<mTitleList.size();i++){
                                        mTitleList.get(i).setSelected(false);
                                    }
                                    mTitleList.get(position).setSelected(true);
                                    adapter.notifyDataSetChanged();
                                    viewPager.setCurrentItem(position);
                                }
                            });
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }


}